/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package maping;

/**
 *
 * @author Jhon
 */
public class OurRectangle {
    private OurPoint point1;
    private OurPoint point2;
    private OurPoint point3;
    private OurPoint point4;
    private int grados;
    private int xPos,yPos,width,height;
    public OurRectangle(int x,int y,int w,int h){
        point1=new OurPoint(x,y);
        point2= new OurPoint(x,y+h);
        point3=new OurPoint(x+w,y+h);
        point4 = new OurPoint(x+w,y);
        xPos=x;
        yPos=y;
        width=w;
        height=h;
        grados=0;
    }

    //este metodo toca mirarlo porq es el q me dice cuales son mis nuevos points
    // del rectangulo por lo q uno modifica es el ancho y el alto.... entonces calcula los nuevos points
    // y alla en la vista pinta los 4 points...

    public void update(){
        //aqui coje los points normalitos que definen el rectangulo
        point1=new OurPoint(xPos,yPos);
        point2= new OurPoint(xPos,yPos+height);
        point3=new OurPoint(xPos+width,yPos+height);
        point4 = new OurPoint(xPos+width,yPos);
        //a partir del os points anteriores rota el rectangulo de la misma forma
        //que cualquier point... traslado/roto/traslado
       double radian = (double)grados*(Math.PI / 180);
        int x,y,x1,y1;
        x=point1.getX()-(xPos+ width/2);
        y=point1.getY()-(yPos+ height/2);
        OurPoint point = Our2DTransform.rotarPoint(x, y, (float)getRadian());
        x1 = point.getX();
        y1 = point.getY();

        x1+=(xPos+ width/2);
        y1+=(yPos+ height/2);
        point1.setX(x1);
        point1.setY(y1);

        x=point2.getX()-(xPos+ width/2);
        y=point2.getY()-(yPos+ height/2);
        point = Our2DTransform.rotarPoint(x, y, (float)getRadian());
        x1 = point.getX();
        y1 = point.getY();
        x1+=(xPos+ width/2);
        y1+=(yPos+ height/2);
        point2.setX(x1);
        point2.setY(y1);

        x=point3.getX()-(xPos+ width/2);
        y=point3.getY()-(yPos+ height/2);
        point = Our2DTransform.rotarPoint(x, y, (float)getRadian());
        x1 = point.getX();
        y1 = point.getY();
        x1+=(xPos+ width/2);
        y1+=(yPos+ height/2);
        point3.setX(x1);
        point3.setY(y1);

        x=point4.getX()-(xPos+ width/2);
        y=point4.getY()-(yPos+ height/2);
        point = Our2DTransform.rotarPoint(x, y, (float)getRadian());
        x1 = point.getX();
        y1 = point.getY();
        x1+=(xPos+ width/2);
        y1+=(yPos+ height/2);
        point4.setX(x1);
        point4.setY(y1);
    }



    public OurPoint[] getPoints(){
        OurPoint[] points = new OurPoint[4];
        points[0]=point1;
        points[1]=point2;
        points[2]=point3;
        points[3]=point4;
        return points;
    }
    public double getRadian(){
        return (double)grados*(Math.PI / 180);
    }

    //esta operacion es para aumentar el giro del rectangulo en un grado...
    //para usar las formulas toca utilizar radianes...
    public void rotateOneDegree(){
        grados++;
        double radian = (double)grados*(Math.PI / 180);
        int x,y,x1,y1;
        x=point1.getX()-(xPos+ width/2);
        y=point1.getY()-(yPos+ height/2);
        OurPoint point = Our2DTransform.rotarPoint(x, y, (float)getRadian());
        x1 = point.getX();
        y1 = point.getY();
        x1+=(xPos+ width/2);
        y1+=(yPos+ height/2);
        point1.setX(x1);
        point1.setY(y1);

        x=point2.getX()-(xPos+ width/2);
        y=point2.getY()-(yPos+ height/2);
        point = Our2DTransform.rotarPoint(x, y, (float)getRadian());
        x1 = point.getX();
        y1 = point.getY();
        x1+=(xPos+ width/2);
        y1+=(yPos+ height/2);
        point2.setX(x1);
        point2.setY(y1);

        x=point3.getX()-(xPos+ width/2);
        y=point3.getY()-(yPos+ height/2);
        point = Our2DTransform.rotarPoint(x, y, (float)getRadian());
        x1 = point.getX();
        y1 = point.getY();
        x1+=(xPos+ width/2);
        y1+=(yPos+ height/2);
        point3.setX(x1);
        point3.setY(y1);

        x=point4.getX()-(xPos+ width/2);
        y=point4.getY()-(yPos+ height/2);
        point = Our2DTransform.rotarPoint(x, y, (float)getRadian());
        x1 = point.getX();
        y1 = point.getY();
        x1+=(xPos+ width/2);
        y1+=(yPos+ height/2);
        point4.setX(x1);
        point4.setY(y1);
    }
    //rotar menos 1 grado...
    public void rotateMinusOneDegree(){
        grados--;
        double radian = (double)grados*(Math.PI / 180);
        int x,y,x1,y1;
        x=point1.getX()-(xPos+ width/2);
        y=point1.getY()-(yPos+ height/2);
        OurPoint point = Our2DTransform.rotarPoint(x, y, (float)getRadian());
        x1 = point.getX();
        y1 = point.getY();
        x1+=(xPos+ width/2);
        y1+=(yPos+ height/2);
        point1.setX(x1);
        point1.setY(y1);

        x=point2.getX()-(xPos+ width/2);
        y=point2.getY()-(yPos+ height/2);
        point = Our2DTransform.rotarPoint(x, y, (float)getRadian());
        x1 = point.getX();
        y1 = point.getY();
        x1+=(xPos+ width/2);
        y1+=(yPos+ height/2);
        point2.setX(x1);
        point2.setY(y1);

        x=point3.getX()-(xPos+ width/2);
        y=point3.getY()-(yPos+ height/2);
        point = Our2DTransform.rotarPoint(x, y, (float)getRadian());
        x1 = point.getX();
        y1 = point.getY();
        x1+=(xPos+ width/2);
        y1+=(yPos+ height/2);
        point3.setX(x1);
        point3.setY(y1);

        x=point4.getX()-(xPos+ width/2);
        y=point4.getY()-(yPos+ height/2);
        point = Our2DTransform.rotarPoint(x, y, (float)getRadian());
        x1 = point.getX();
        y1 = point.getY();
        x1+=(xPos+ width/2);
        y1+=(yPos+ height/2);
        point4.setX(x1);
        point4.setY(y1);
    }

    /**
     * @return the xPos
     */
    public int getxPos() {
        return xPos;
    }

    /**
     * @param xPos the xPos to set
     */
    public void setxPos(int xPos) {
        this.xPos = xPos;
    }

    /**
     * @return the yPos
     */
    public int getyPos() {
        return yPos;
    }

    /**
     * @param yPos the yPos to set
     */
    public void setyPos(int yPos) {
        this.yPos = yPos;
    }

    /**
     * @return the width
     */
    public int getWidth() {
        return width;
    }

    /**
     * @param width the width to set
     */
    public void setWidth(int width) {
        this.width = width;
    }

    /**
     * @return the height
     */
    public int getHeight() {
        return height;
    }

    /**
     * @param height the height to set
     */
    public void setHeight(int height) {
        this.height = height;
    }
}
